package com.play.ballen.home.vm

import androidx.lifecycle.MutableLiveData
import com.play.ballen.R
import com.play.ballen.home.entity.BottleData
import com.play.ballen.home.entity.GasInfo
import com.play.ballen.home.entity.HomeIndexInfo
import com.play.ballen.home.entity.PlayCountInfo
import com.play.ballen.home.entity.Version
import com.play.ballen.network.ApiRepository
import com.play.ballen.network.BallenRxHelper
import com.play.ballen.utils.UserInfoManager
import com.yuven.appframework.busevent.SingleLiveEvent
import com.yuven.appframework.network.ExceptionHandle
import com.yuven.appframework.network.RxSchedulerHelper
import com.yuven.appframework.util.addToDisposable
import com.yuven.baselib.component.viewmodel.BaseViewModel
import com.yuven.baselib.utils.Logger
import com.yuven.baselib.utils.ResourceUtil

class HomeFragmentVM : BaseViewModel() {

    private val model = ApiRepository()
    val homeIndexInfo = MutableLiveData<HomeIndexInfo?>()
    val actionIndexFail = SingleLiveEvent<Void>()

    val playCountInfo = SingleLiveEvent<PlayCountInfo?>()
    val gasInfo = SingleLiveEvent<GasInfo?>()

    val version = MutableLiveData<Version>()
    val bottleData = MutableLiveData<BottleData>()

    fun index() {
        model.index()
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .subscribe({
                Logger.i("HomeFragmentVM.index success")
                homeIndexInfo.value = it
            }, { throwable ->
                toastLiveData.value = throwable.message
                actionIndexFail.call()
                Logger.i("HomeFragmentVM.index error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

    fun task() {
        showDialog.setValue(true, ResourceUtil.getString(R.string.loading), false)
        model.task()
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                showDialog.setValue(false)
            }
            .subscribe({
                Logger.i("HomeFragmentVM.task success")
                playCountInfo.value = it
            }, { throwable ->
                if (throwable is ExceptionHandle.ServerException) {
                    if (throwable.code == "2") {    //需要支付燃料
                        Logger.i("HomeFragmentVM.task code==2, need to pay")
                        gas()
                        return@subscribe
                    }
                }
                toastLiveData.value = throwable.message
                Logger.i("HomeFragmentVM.task error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

    private fun gas() {
        showDialog.setValue(true, ResourceUtil.getString(R.string.loading))
        model.gas()
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                showDialog.setValue(false)
            }
            .subscribe({
                Logger.i("HomeFragmentVM.gas success")
                gasInfo.value = it
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("HomeFragmentVM.gas error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

    fun gasSubmit() {
        showDialog.setValue(true, ResourceUtil.getString(R.string.loading))
        model.gasSubmit()
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                showDialog.setValue(false)
            }
            .subscribe({
                toastLiveData.value = ResourceUtil.getString(R.string.play_count_pay_gas_success)
                Logger.i("HomeFragmentVM.gasSubmit success")
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("HomeFragmentVM.gasSubmit error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

    fun getVersion(id: String) {
//
    }
    fun getUserInfo(){
        model.getUserInfo()
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .subscribe({
//                loginIm(it,userInfo);
                UserInfoManager.setUserInfo(it)
            }, {
                UserInfoManager.logout()
                toastLiveData.value ="登录失败，请重试"
            }).addToDisposable(compositeDisposable)
    }


}